package com.emspost.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
//        String s1 = "runoob";
//        String s2 = "runoob";
//        System.out.println("s1 == s2 is:" + s1 == s2);

        String calculateTime = "5,6,7";
        calculateTime = formatWeekCalculateTime(calculateTime);

        System.out.println(String.format("0 0 4 ? * %s", calculateTime));

    }

    private static String formatWeekCalculateTime(String calculateTime) {
        String[] split = calculateTime.split(",");
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            result.append(Integer.parseInt(split[i]) + 1 == 8 ? "1" : Integer.valueOf(split[i]) + 1 + "");
            if (i < split.length - 1) {
                result.append(",");
            }
        }
        calculateTime = result.toString();
        return calculateTime;
    }

//    public ResultMap<TouchTargetCompleteResp> touchTargetComplete(TouchBatchPageReq req) {
//        TouchTargetCompleteResp touchTargetCompleteResp = new TouchTargetCompleteResp();
//        TouchActivity touchActivity = touchActivityService.getById(req.getActivityId());
//        List<Long> completeCountList = new ArrayList<>();
//        if (touchActivity.getFinishTime() != null) {
//            //计算首要目标
//            String targetMain = touchActivity.getTargetMain();
//            if (StrUtil.isNotBlank(targetMain)) {
//                Long mainTargetCompleteCount = this.getTargetCompleteCount(targetMain,touchActivity);
//                completeCountList.add(mainTargetCompleteCount);
//            }
//            //计算次要目标1
//            String targetSecondary1 = touchActivity.getTargetSecondary1();
//            if (StrUtil.isNotBlank(targetSecondary1)) {
//                Long targetSecondary1CompleteCount = this.getTargetCompleteCount(targetSecondary1,touchActivity);
//                completeCountList.add(targetSecondary1CompleteCount);
//            }
//            //计算次要目标2
//            String targetSecondary2 = touchActivity.getTargetSecondary2();
//            if (StrUtil.isNotBlank(targetSecondary2)) {
//                Long targetSecondary2CompleteCount = this.getTargetCompleteCount(targetSecondary2,touchActivity);
//                completeCountList.add(targetSecondary2CompleteCount);
//            }
//            //计算次要目标3
//            String targetSecondary3 = touchActivity.getTargetSecondary3();
//            if (StrUtil.isNotBlank(targetSecondary3)) {
//                Long targetSecondary3CompleteCount = this.getTargetCompleteCount(targetSecondary3,touchActivity);
//                completeCountList.add(targetSecondary3CompleteCount);
//            }
//        }
//        touchTargetCompleteResp.setCompleteCountList(completeCountList);
//        return ResultUtil.success(touchTargetCompleteResp);
//    }
//
//
//    /**
//     * 获取目标完成人数
//     * @param target
//     * @return
//     */
//    private Long getTargetCompleteCount(String target,TouchActivity touchActivity) {
//        List<ConditionFilterVo> userConditionFilter = new ArrayList<>();
//        List<ConditionFilterVo> eventConditionFilter = new ArrayList<>();
//        TouchUpTargetVo touchUpTargetVo = JSON.parseObject(target, TouchUpTargetVo.class);
//        if (CollectionUtil.isNotEmpty(touchUpTargetVo.getConditionFilterList())) {
//            for (ConditionFilterVo conditionFilter : touchUpTargetVo.getConditionFilterList()) {
//                if (AttributeTypeEnum.USER.getCode().equals(conditionFilter.getType())) {
//                    userConditionFilter.add(conditionFilter);
//                }
//                if (AttributeTypeEnum.EVENT.getCode().equals(conditionFilter.getType())) {
//                    eventConditionFilter.add(conditionFilter);
//                }
//            }
//        }
//        //获取分区字段的时间
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
//        Integer startTimePartition = Integer.parseInt(sdf.format(touchActivity.getCreateTime()));
//        Integer endTimePartition = null;
//        if(TimeTypeEnum.MINUTE.getCode().equals(touchUpTargetVo.getTimeType())) {
//            endTimePartition = Integer.parseInt(sdf.format(cn.hutool.core.date.DateUtil.offsetMinute(touchActivity.getFinishTime(),touchUpTargetVo.getDuringTime())));
//        } else if(TimeTypeEnum.HOUR.getCode().equals(touchUpTargetVo.getTimeType())) {
//            endTimePartition = Integer.parseInt(sdf.format(cn.hutool.core.date.DateUtil.offsetHour(touchActivity.getFinishTime(),touchUpTargetVo.getDuringTime())));
//        } else if(TimeTypeEnum.DAY.getCode().equals(touchUpTargetVo.getTimeType())) {
//            endTimePartition = Integer.parseInt(sdf.format(DateUtil.offsetDay(touchActivity.getFinishTime(),touchUpTargetVo.getDuringTime())));
//        }
//        Long targetCompleteCount = baseMapper.selectTargetCompleteCount(touchUpTargetVo,
//                touchActivity.getId(),
//                userConditionFilter,
//                eventConditionFilter,
//                startTimePartition,
//                endTimePartition);
//        return targetCompleteCount;
//    }


//    public void downloadTagInfoUser(DownloadTagInfoUserReq req, HttpServletResponse response) {
//        // 查询标签是否存在
//        TagInfo tagInfo = tagInfoMapper.selectTagInfoById(req.getTagId());
//        if (tagInfo == null) {
//            throw new EucTagSvcException(TagSvcSysMsg.DATA_NOT_EXIST);
//        }
//        // 创建分页对象
//        // 假设每页1000条数据
//        int pageSize = 5000;
//        int currentPage = 1;
//        try {
//            // 设置响应头
////            response.setContentType("application/vnd.ms-excel");
//            response.setContentType("text/csv");
//            response.setCharacterEncoding("utf-8");
//            String fileName = URLEncoder.encode("标签用户数据", "UTF-8");
////            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".csv");
//
//            // 创建Excel写入器
//            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), PageUserListVo.class).build();
//            WriteSheet writeSheet = EasyExcel.writerSheet("用户数据").build();
//
//            // 循环分页查询所有数据
//            while (true) {
//                Page<PageUserListVo> page = new Page<>(currentPage, pageSize);
//                PageUserListDto pageUserListDto = new PageUserListDto();
//                pageUserListDto.setTagId(req.getTagId());
//                pageUserListDto.setMobile(req.getMobile());
//                // 分页查询数据
//                Page<PageUserListVo> userListPage = userService.selectUserListByPage(page, pageUserListDto);
//                List<PageUserListVo> dataList = userListPage.getRecords();
//                if (CollUtil.isEmpty(dataList)) {
//                    break; // 如果没有数据，退出循环
//                }
//                // 将数据写入Excel
//                excelWriter.write(dataList, writeSheet);
//                currentPage++; // 继续下一页
//            }
//            // 关闭Excel写入器
//            excelWriter.finish();
//        } catch (IOException e) {
//            log.error("导出标签用户数据失败", e);
//            throw new EucTagSvcException(TagSvcSysMsg.EXPORT_FAILED);
//        }
//    }


//        <select id="selectTargetCompleteCount" resultType="java.lang.Long">
//    select count(distinct pe.userid)
//    from
//            (select userid,eventtriggertime from pa_analysis_event  where eventcode = #{touchUpTargetVo.eventCode}
//    and triggerdate between ${startTimePartition} and ${endTimePartition}
//        <if test="eventConditionFilter != null and eventConditionFilter.size() != 0">
//            <foreach item="filter" collection="eventConditionFilter">
//    and ${filter.field} ${filter.operate}
//                <if test="filter.value != null and filter.value != ''">
//                    <if test="filter.dataType == 'DATE'">
//    STR_TO_DATE(#{filter.value}, '%Y-%m-%d')
//                    </if>
//                    <if test="filter.dataType != 'DATE'">
//            #{filter.value}
//                    </if>
//                </if>
//            </foreach>
//        </if>
//            ) pe
//            where
//    exists
//            (select 1 from pa_touch_record tr where status = 1 and activity_id =#{activityId}
//    and pe.eventtriggertime between tr.create_time
//    and date_add('${touchUpTargetVo.timeType}', #{touchUpTargetVo.duringTime},tr.create_time)
//    and pe.userid =tr.userid
//    and pe.userid between tr.userid and tr.userid
//        )
//        <if test="userConditionFilter != null and userConditionFilter.size() != 0">
//    and
//    exists
//            (select 1 from pa_user_field uf where
//                    pe.userid =uf.userid
//                    and pe.userid between uf.userid and uf.userid
//                    <foreach item="filter" collection="userConditionFilter">
//                    and ${filter.field} ${filter.operate}
//                <if test="filter.value != null and filter.value != ''">
//                    <if test="filter.dataType == 'DATE'">
//    STR_TO_DATE(#{filter.value}, '%Y-%m-%d')
//                    </if>
//                    <if test="filter.dataType != 'DATE'">
//            #{filter.value}
//                    </if>
//                </if>
//            </foreach>
//            )
//        </if>
//    </select>
//
//
//    SELECT COUNT(DISTINCT scan.userId) AS user_count
//    FROM your_table scan
//    JOIN your_table reg ON scan.userId = reg.userId
//    AND reg.eventCode = 'register'
//    AND reg.eventTriggerTime BETWEEN scan.eventTriggerTime AND DATE_ADD(scan.eventTriggerTime, INTERVAL 3 DAY)
//    WHERE scan.eventCode = 'scanqrcode';

}